from django.core.paginator import PageNotAnInteger
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import render
from utils.mixin_utils import LoginRequiredMixin

from django.views import View
from pure_pagination import Paginator

from course.models import Course, CourseResource, Video
from operation.models import UserFavorite, CourseComments, UserCourse


class CourseListView(View):
    '''课程列表'''
    def get(self, request):
        all_courses = Course.objects.all().order_by('-add_time')
        # 热门课程推荐
        hot_courses = Course.objects.all().order_by('-click_nums')[:3]
        # 全局搜索
        search_keywords = request.GET.get('keywords', '')
        if search_keywords:
            # i代表不区分大小写，contians代表like语句操作
            # or操作使用Q
            all_courses = all_courses.filter(Q(name__icontains=search_keywords) | Q(desc__icontains=search_keywords) | Q(detail__icontains=search_keywords))

        # 排序
        sort = request.GET.get('sort', '')
        if sort:
            if sort == 'students':
                all_courses = all_courses.order_by("-students")
            elif sort == 'hot':
                all_courses = all_courses.order_by("-click_nums")

        # 分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(all_courses, 6, request=request)
        courses = p.page(page)
        return render(request, 'course-list.html', {
            'all_courses': courses,
            'sort': sort,
            'hot_courses': hot_courses
        })


class CourseDetailView(View):
    '''课程详情'''
    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        # 课程点击数+1
        course.click_nums += 1
        course.save()
        # 课程标签
        # 通过课程标签，查找课程
        tag = course.tag
        if tag:
            # 需要从1开始，不然自己推荐
            relate_courses = Course.objects.filter(tag=tag)[:3]
        else :
            relate_courses = []

        # 判断用户是否收藏本课程或机构
        # 初始化
        has_fav_course = False
        has_fav_org = False
        # 判断用户是否登录
        if request.user.is_authenticated:
            if UserFavorite.objects.filter(user=request.user, fav_id=course_id, fav_type=1):
                has_fav_course = True
            if UserFavorite.objects.filter(user=request.user, fav_id=course.course_org.id, fav_type=2):
                has_fav_org = True

        return render(request, 'course-detail.html', {
            'course': course,
            'relate_courses': relate_courses,
            'has_fav_course': has_fav_course,
            'has_fav_org': has_fav_org
        })


class CourseInfoView(LoginRequiredMixin, View):
    '''课程章节信息'''
    def get(self,request, course_id):
        course = Course.objects.get(id=int(course_id))
        course.students += 1
        course.save()
        # 查看用户是否学历该课程
        user_course = UserCourse.objects.filter(user=request.user, course=course)
        if not user_course:
            # 如果没有学习就关联起来
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()

        # 相关课程推荐
        # 查找学习此课程的全部用户
        user_courses = UserCourse.objects.filter(course=course)
        # 获取学习课程的用户id
        user_ids = [user_course.user_id for user_course in user_courses]
        # 通过用户id查找所有用户学习过的课程
        all_user_courses = UserCourse.objects.filter(user_id__in=user_ids)
        # 获取课程的id
        course_ids = [all_user_course.course_id for all_user_course in all_user_courses]
        # 通过课程id，查找所有课程，去点击量前5个
        relate_courses = Course.objects.filter(id__in=course_ids).order_by('-click_nums')[:5]

        # 课程资源
        all_resources = CourseResource.objects.filter(course=course)
        return render(request, 'course-video.html', {
            'course': course,
            'all_resources': all_resources,
            'relate_courses': relate_courses
        })


class VideoPlayView(LoginRequiredMixin, View):
    '''课程章节视频播放页面'''
    def get(self, request, video_id):
        video = Video.objects.get(id=int(video_id))
        # 通过外键查找到章节，在找到视频对应的课程
        course = video.lesson.course
        course.students += 1
        course.save()

        # 查询用户是否学习了该课程
        user_courses = UserCourse.objects.filter(user=request.user, course=course)
        if not user_courses:
            # 如果没有学习课程就关联起来
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()

        # 相关课程推荐
        # 找到学习这么课程的全部用户
        user_courses = UserCourse.objects.filter(course=course)
        # 找到学习课程的用户id
        user_ids = [user_course.user_id for user_course in user_courses]
        # 通过用户id查找所有用户学习的课程
        all_user_courses = UserCourse.objects.filter(user_id__in=user_ids)
        # 取出所有课程id
        course_ids = [all_user_course.course_id for all_user_course in all_user_courses]
        # 通过课程id查找课程，根据点击量取前5
        relate_courses = Course.objects.filter(id__in=course_ids).order_by('-click_nums')[:5]

        # 课程资源
        all_resources = CourseResource.objects.filter(course=course)
        return render(request, "course-play.html", {
            'course': course,
            'all_resources': all_resources,
            'relate_courses': relate_courses,
            'video': video
        })


class CommentsView(View):
    '''课程评论'''
    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        all_resources = CourseResource.objects.filter(course=course)
        all_comments = CourseComments.objects.all()
        return render(request, 'course-comment.html', {
            'course': course,
            'all_resources': all_resources,
            'all_comments': all_comments
        })


class AddCommentsView(View):
    '''添加评论'''
    def post(self,request):
        # 判断用户是否登录
        if not request.user.is_authenticated:
            return  HttpResponse('{"status":"fail","msg":"用户未登录"}', content_type='application/json')
        course_id = request.POST.get('course_id',0)
        comments = request.POST.get('comments','')
        if int(course_id)>0 and comments:
            # 实例化一个course_comments对象
            course_comments = CourseComments()
            # 获取评论的课程
            course = Course.objects.get(id=int(course_id))
            course_comments.course = course
            course_comments.user = request.user
            course_comments.comments = comments
            course_comments.save()
            return HttpResponse('{"status":"success", "msg":"评论成功"}', content_type='application/json')
        else:
            return HttpResponse('{"status":"fail", "msg":"评论失败"}', content_type='application/json')